package com.li.test.base;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockSynchronized {
    int num = 0;
    int synum = 0;
    public synchronized void addsy() {
        synum++;
    }
    public void addPlus() {
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
            num++;

            System.out.println("当前线程名："+Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        LockSynchronized los = new LockSynchronized();
        for (int j = 1; j <= 20; j++) {
            new Thread(() -> {
                for (int i = 0; i < 1000; i++) {
                    los.addPlus();
                    los.addsy();
                }
            }).start();
        }
        if (Thread.activeCount() > 1) {
            Thread.yield();
        }
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("输出：" + los.num);
        System.out.println("输出2：" + los.synum);

    }
}
